package kitty.main;

import java.math.BigDecimal;

import com.example.kittyvszombie.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;

public class Interface {
	private int touchCordY, touchCordX, selection, xSquare, ySquare,
			selectedInfo;
	private boolean showGrid, showScndSelection, switchCards, cardsGoDown,
			showInfo;
	private float cardSwapSpeed, cardsYPos, card1x, card2x, card3x, dp;
	private double delta;
	private Kitty[][] kittyGrid;
	private MainView view;
	private Paint paint;
	private Bitmap background, cardsBackground, cardSelectedBackground, card1,
			card2, card3, card4, card5, card6, grid, cardArrow, arrow1, arrow2,
			infoCard1, infoCard2, infoCard3, infoCard4, infoCard5, infoCard6,
			infoDot;
	private int catCostStandardKitty = 3;
	private int catCostStarKitty = 3;
	private int catCostEmoKitty = 7;
	private int catCostFoodKitty = 5;
	private int catCostCuteKitty = 7;
	private int catCostBatKitty = 15;

	public Interface(MainView view) {
		this.view = view;
		this.kittyGrid = view.getKittyGrid();
		this.dp = view.getDp();
		loadBitmaps(view);
		cardSwapSpeed = 200.0f;
		cardsYPos = 0;

		paint = new Paint();
		paint.setFakeBoldText(true);
		paint.setColor(Color.BLACK);
		paint.setTextSize(30);

		// set card positions
		card1x = 190 * dp;
		card2x = 190 * dp + cardsBackground.getWidth();
		card3x = 190 * dp + cardsBackground.getWidth() * 2;

	}

	private void loadBitmaps(MainView view) {
		background = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.interf_background);
		cardsBackground = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.card_background);
		cardSelectedBackground = BitmapFactory.decodeResource(
				view.getResources(), R.drawable.card_background_selected);
		grid = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.grid);
		card1 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.kitty_card);
		card2 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.starkitty_card);
		card3 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.emokitty_card);
		card4 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.foodkitty_card);
		card5 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.cutekitty_card);
		card6 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.batkitty_card);

		infoCard1 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.infostandard);
		infoCard2 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.infostarkitty);
		infoCard3 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.infoemokitty);
		infoCard4 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.infofoodkitty);
		infoCard5 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.infocute);
		infoCard6 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.infobatcat);
		infoDot = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.infoicon);
		arrow1 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.arrow1);

		arrow2 = BitmapFactory.decodeResource(view.getResources(),
				R.drawable.arrow2);

		cardArrow = arrow1;
	}

	public void render(Canvas canvas) {
		// RITAAAA

		canvas.drawBitmap(background, 50 * dp, 0, null);
		canvas.drawBitmap(cardArrow, 525 * dp, 5 * dp, null);
		renderCards(canvas);
		drawKittyInfo(canvas);
		canvas.drawText(view.getNumberOfStars() + " STARS", 110 * dp, 40 * dp,
				paint);

		canvas.drawText("Time: " + view.getElapsedTime(), 110 * dp, 70 * dp,
				paint);

		if (showGrid == true) {
			canvas.drawBitmap(grid, 200, 350, null);
			// drawTouchCord(canvas);
		}
	}

	private void drawKittyInfo(Canvas canvas) {
		// draw infodots
		canvas.drawBitmap(infoDot, card1x, cardsYPos, null);
		canvas.drawBitmap(infoDot, card2x, cardsYPos, null);
		canvas.drawBitmap(infoDot, card3x, cardsYPos, null);

		if (showInfo) {
			switch (selectedInfo) {
			case 1:
				canvas.drawBitmap(infoCard1, card1x, cardsYPos + 190, null);
				break;
			case 2:
				canvas.drawBitmap(infoCard2, card2x, cardsYPos + 190, null);
				break;
			case 3:
				canvas.drawBitmap(infoCard3, card3x, cardsYPos + 190, null);
				break;
			case 4:
				canvas.drawBitmap(infoCard4, card1x, cardsYPos + 190, null);
				break;
			case 5:
				canvas.drawBitmap(infoCard5, card2x, cardsYPos + 190, null);
				break;
			case 6:
				canvas.drawBitmap(infoCard6, card3x, cardsYPos + 190, null);
				break;
			}
		}

	}

	private void renderCards(Canvas canvas) {
		canvas.drawBitmap(cardsBackground, card1x, cardsYPos, null);
		canvas.drawBitmap(cardsBackground, card2x, cardsYPos, null);
		canvas.drawBitmap(cardsBackground, card3x, cardsYPos, null);

		if (selection == 1 || selection == 4) {
			canvas.drawBitmap(cardSelectedBackground, card1x, 0, null);
		}

		else if (selection == 2 || selection == 5) {
			canvas.drawBitmap(cardSelectedBackground, card2x, 0, null);
		}

		else if (selection == 3 || selection == 6) {
			canvas.drawBitmap(cardSelectedBackground, card3x, 0, null);
		}

		if (!showScndSelection) {
			canvas.drawBitmap(card1, card1x, cardsYPos, null);
			canvas.drawBitmap(card2, card2x, cardsYPos, null);
			canvas.drawBitmap(card3, card3x, cardsYPos, null);
		}

		else {
			canvas.drawBitmap(card4, card1x, cardsYPos, null);
			canvas.drawBitmap(card5, card2x, cardsYPos, null);
			canvas.drawBitmap(card6, card3x, cardsYPos, null);
		}
	}

	public void update() {
		// UPPDATERA
		delta = view.getDelta();

		switchCards();
	}

	public void onTouchEvent(MotionEvent event) {
		touchCordY = (int) event.getY();
		touchCordX = (int) event.getX();

		checkCardTouch();

		checkArrowTouch();

		checkGridTouch();

		checkInfoTouch();
	}

	private void checkInfoTouch() {
		// checkInfoTouch
		showInfo = false;
		if (touchCordX > card1x && touchCordX < card1x + infoDot.getWidth()
				&& touchCordY > cardsYPos
				&& touchCordY < cardsYPos + infoDot.getHeight()) {
			if (!showScndSelection)
				selectedInfo = 1;
			else
				selectedInfo = 4;
			showInfo = true;
		} else if (touchCordX > card2x
				&& touchCordX < card2x + infoDot.getWidth()
				&& touchCordY > cardsYPos
				&& touchCordY < cardsYPos + infoDot.getHeight()) {
			if (!showScndSelection)
				selectedInfo = 2;
			else
				selectedInfo = 5;
			showInfo = true;
		}

		else if (touchCordX > card3x
				&& touchCordX < card3x + infoDot.getWidth()
				&& touchCordY > cardsYPos
				&& touchCordY < cardsYPos + infoDot.getHeight()) {
			if (!showScndSelection)
				selectedInfo = 3;
			else
				selectedInfo = 6;
			showInfo = true;
		}
	}

	private void checkArrowTouch() {
		// trycka p� pilen
		if ((touchCordX > 190 * dp + cardsBackground.getWidth() * 3 && touchCordX < view
				.getWidth())
				&& (touchCordY < cardsBackground.getHeight())
				&& !showGrid) {
			switchCards = true;
			view.playSwapSound();

			if (showScndSelection) {
				cardArrow = arrow1;
			} else {
				cardArrow = arrow2;
			}
		}
	}

	private void checkGridTouch() {
		// spelplanens rutn�t touch
		if ((touchCordX > 200 && touchCordX < 1000)
				&& (touchCordY < 650 && touchCordY > 350)) {
			xSquare = touchCordX / 100 - 1;
			if (touchCordY < 450) {
				ySquare = 0;
			} else if (touchCordY < 550) {
				ySquare = 1;
			} else {
				ySquare = 2;
			}
			System.out.println("Touched!: " + "X: " + xSquare + " Y: "
					+ ySquare);

			placeKittyOnGrid();
		}
	}

	private void placeKittyOnGrid() {
		if (showGrid && kittyGrid[ySquare][xSquare] == null) {
			// r�kna ut position
			int x = 100 + (100 * xSquare);
			int y = 300 + (100 * ySquare);

			switch (selection) {
			case 1:
				if (view.getNumberOfStars() >= catCostStandardKitty) {
					kittyGrid[ySquare][xSquare] = new StandardKitty(view,
							ySquare, x, y);
					view.addStars(-catCostStandardKitty);
					view.playGridSound();
				}
				break;
			case 2:
				if (view.getNumberOfStars() >= catCostStarKitty) {
					kittyGrid[ySquare][xSquare] = new StarKitty(view, ySquare,
							x, y);
					view.addStars(-catCostStarKitty);
					view.playGridSound();
				}
				break;
			case 3:
				if (view.getNumberOfStars() >= catCostEmoKitty) {
					kittyGrid[ySquare][xSquare] = new EmoKitty(view, ySquare,
							x, y);
					view.addStars(-catCostEmoKitty);
					view.playGridSound();
				}
				break;
			case 4:
				if (view.getNumberOfStars() >= catCostFoodKitty) {
					kittyGrid[ySquare][xSquare] = new FoodKitty(view, ySquare,
							x, y);
					view.addStars(-catCostFoodKitty);
					view.playGridSound();
				}
				break;
			case 5:
				if (view.getNumberOfStars() >= catCostCuteKitty) {
					kittyGrid[ySquare][xSquare] = new CuteKitty(view, ySquare,
							x, y);
					view.addStars(-catCostCuteKitty);
					view.playGridSound();
				}
				break;
			case 6:
				if (view.getNumberOfStars() >= catCostBatKitty) {
					kittyGrid[ySquare][xSquare] = new BatCat(view, ySquare, x,
							y);
					view.addStars(-catCostBatKitty);
					view.playGridSound();
				}
				break;
			}
			// �terst�ll variablerna
			selection = 0;
			showGrid = false;
		}
	}

	private void checkCardTouch() {
		// V�lja f�rsta rutan
		if ((touchCordX > 380 && touchCordX < 380 + cardsBackground.getWidth())
				&& (touchCordY < cardsBackground.getHeight())) {

			if (selection == 1 || selection == 4) {
				selection = 0;
				showGrid = false;
			} else {
				if (!showScndSelection) {
					selection = 1;
				} else {
					selection = 4;
				}
				showGrid = true;
			}
		}

		// andra rutan
		else if ((touchCordX > 380 + cardsBackground.getWidth() && touchCordX < 380 + cardsBackground
				.getWidth() * 2) && (touchCordY < cardsBackground.getHeight())) {

			if (selection == 2 || selection == 5) {
				selection = 0;
				showGrid = false;
			} else {
				if (!showScndSelection) {
					selection = 2;
				} else {
					selection = 5;
				}
				showGrid = true;
			}
		}

		// tredje rutan
		else if ((touchCordX > 380 + cardsBackground.getWidth() * 2 && touchCordX < 380 + cardsBackground
				.getWidth() * 3) && (touchCordY < cardsBackground.getHeight())) {

			if (selection == 3 || selection == 6) {
				selection = 0;
				showGrid = false;
			} else {
				if (!showScndSelection) {
					selection = 3;
				} else {
					selection = 6;
				}

				showGrid = true;
			}
		}
	}

	private void drawGrid(Canvas canvas) {
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);

		Rect r = new Rect();
		r.set(200, 350, 1000, 650);
		paint.setColor(Color.BLACK);
		paint.setAlpha(40);
		canvas.drawRect(r, paint);
		paint.setColor(Color.BLACK);
		paint.setStrokeWidth(3);
		paint.setAlpha(100);
		for (int i = 0; i < 4; i++) {
			canvas.drawLine(200, 350 + i * 100, 1000, 350 + i * 100, paint);
		}
		for (int i = 0; i < 9; i++) {
			canvas.drawLine(200 + i * 100, 350, 200 + i * 100, 650, paint);
		}
	}

	private void drawTouchCord(Canvas canvas) {
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTextSize(30);
		canvas.drawText("C: " + ySquare + xSquare, 320, 220, paint);
	}

	public void restart() {
		showScndSelection = false;
	}

	private void switchCards() {
		if (switchCards) {

			if (!cardsGoDown) {
				cardsYPos -= cardSwapSpeed * delta;
			}

			else if (cardsGoDown) {
				cardsYPos += cardSwapSpeed * delta;
			}

			if (cardsYPos < -cardsBackground.getHeight()) {
				showScndSelection = !showScndSelection;
				cardsGoDown = true;
			}

			if (cardsYPos >= 0) {
				cardsYPos = 0;
				switchCards = false;
				cardsGoDown = false;
			}
		}
	}

}
